package tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Description:
 * @Author: yjj
 * @Date: 2025/8/15 17:00
 */
public class Main3 {
    public static void main(String[] args) {
        String s = "{\n" +
                "  \"serverIds\": [\n" +
                "    [\n" +
                "      \"4760566730416939139\"\n" +
                "    ],\n" +
                "    \"4760566730416939112\"\n" +
                "  ]\n" +
                "}";
        List<String> list = Collections.singletonList("serverIds");
        Map<String, List<Object>> valueMap = getValueMap(s, list);
        System.out.println(valueMap);
        for (Map.Entry<String, List<Object>> entry : valueMap.entrySet()) {
            List<Object> valueList = entry.getValue();
            if (!CollectionUtils.isEmpty(valueList)) {
                for (Object o : valueList) {
                    try {
                        Long instanceId = Long.valueOf(o.toString());
                        System.out.println(instanceId);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        }

    }

    public static Map<String, List<Object>> getValueMap(String jsonStr, List<String> keyStrs) {
        Map<String, List<Object>> map = new HashMap<>();
        for (String keyStr : keyStrs) {
            List<Object> list = new ArrayList<>();
            Object obj = JSON.parse(jsonStr);
            if (obj instanceof JSONObject) {
                JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                list = getValueByKeyStr(jsonObject, keyStr);
            } else if (obj instanceof JSONArray) {
                JSONArray jsonArray = JSONObject.parseArray(jsonStr);
                for (Object o : jsonArray) {
                    JSONObject jo = (JSONObject) o;
                    list.addAll(getValueByKeyStr(jo, keyStr));
                }
            }
            map.put(keyStr, list);
        }
        return getAllKeyLeafValues(map);
    }

    //取出key所有的叶子元素
    public static Map<String, List<Object>> getAllKeyLeafValues(Map<String, List<Object>> map) {
        Map<String, List<Object>> rMap = new HashMap<>();
        for (String key : map.keySet()) {
            List<Object> list = new ArrayList<>();
            for (Object o : map.get(key)) {
                list.addAll(extractLeafValues(o));
            }
            rMap.put(key, list);
        }
        return rMap;
    }

    /**
     * 递归遍历 JSONArray 或 JSONObject，提取所有叶子节点的值
     *
     * @param obj 可以是 JSONArray 或 JSONObject
     * @return 包含所有叶子元素值的列表
     */
    public static List<Object> extractLeafValues(Object obj) {
        List<Object> leaves = new ArrayList<>();
        if (obj instanceof JSONArray) {
            JSONArray array = (JSONArray) obj;
            for (Object item : array) {
                leaves.addAll(extractLeafValues(item)); // 递归处理数组中的每个元素
            }
        } else if (obj instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) obj;
            for (String key : jsonObject.keySet()) {
                Object value = jsonObject.get(key);
                leaves.addAll(extractLeafValues(value)); // 递归处理对象中的每个值
            }
        } else {
            // 如果既不是数组也不是对象，则是叶子节点
            leaves.add(obj);
        }
        return leaves;
    }

    public static List<Object> getValueByKeyStr(JSONObject jsonObject, String keyStr) {
        List<Object> list = new ArrayList<>();
        Stack<Object> stack = new Stack<>();
        stack.push(jsonObject); // 将根对象压入栈中
        while (!stack.isEmpty()) {
            Object current = stack.pop(); // 弹出栈顶元素
            if (current instanceof JSONObject) {
                // 如果是 JSONObject，获取所有键值对并压入栈中
                JSONObject obj = (JSONObject) current;
                for (Map.Entry<String, Object> entry : obj.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    if (key.equals(keyStr)) {
                        list.add(value);
                    }
                    // 如果值是 JSONObject 或 JSONArray，压入栈中
                    if (value instanceof JSONObject || value instanceof JSONArray) {
                        stack.push(value);
                    }
                }
            } else if (current instanceof JSONArray) {
                // 如果是 JSONArray，遍历数组中的每个元素并压入栈中
                JSONArray array = (JSONArray) current;
                for (Object item : array) {
                    // 如果元素是 JSONObject 或 JSONArray，压入栈中
                    if (item instanceof JSONObject || item instanceof JSONArray) {
                        stack.push(item);
                    }
                }
            }
        }
        return list;
    }
}
